package hadoopLearn.Else;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class CourseNameOutputFormat extends FileOutputFormat<Text, IntWritable> {
    public RecordWriter<Text, IntWritable> getRecordWriter(TaskAttemptContext job)
        throws IOException, InterruptedException
    {
        Path outputDir = FileOutputFormat.getOutputPath(job);
        //获得reduce任务ID
        String reduceId = job.getTaskAttemptID().getTaskID().toString();
        //获得文件系统对象
        FileSystem fs = outputDir.getFileSystem(job.getConfiguration());
        if(reduceId.contains("r_000000")){
            FSDataOutputStream course1 =
                    fs.create(new Path(outputDir.toString() + "/math.txt"));
            return new CourseRecordWriter(course1);
        }
        else if(reduceId.contains("r_000001")){
            FSDataOutputStream course2 =
                    fs.create(new Path(outputDir.toString() + "/english.txt"));
            return new CourseRecordWriter(course2);
        }
        return new CourseRecordWriter();
    }
}
